<template>
  <div class="layout-pd">
      <zh-table title="" ref="queryTable"  size="mini" funcCode="genTable0001" :columns="pageData.tableColumns" highlight-current-row>
        <template #query>
          <zh-form :model="pageData.queryParams" ref="queryFormRef" label-width="80px" :span="6">
            <zh-form-input
                prop="tableName"
                label="表名"
                v-model="pageData.queryParams.tableName"
            ></zh-form-input>
            <zh-form-select
                prop="datasourceId"
                label="数据源"
                v-model="pageData.queryParams.datasourceId"
                :lazy="true"
                func-code="genDatasource0001"
                :fieldNames="{ label: 'connName', value: 'id' }"
            ></zh-form-select>
            <zh-form-query-button @queryClick="refreshTable"  :span="12">
              <zh-button button-type="add" @click="showAdd()" v-func:genTable0004>导入</zh-button>  
            </zh-form-query-button>
          </zh-form>
        </template>
        <template #flag_default="{ row }">
          <zh-badge :status="statusMap[row.status].status" :text="statusMap[row.status].text" />
        </template>
        <template #operate="{ row }">
         
          <zh-button plain size="small" color="#E6A23C" @click="editHandle(row.id)">编辑</zh-button>
          <zh-button plain size="small" color="#409EFF" @click="previewHandle(row.id)">预览</zh-button>
          <zh-button plain size="small" color="#4ec752" @click="generatorHandle(row.id)">生成代码</zh-button>
          <zh-button plain size="small" color="#E6A23C" @click="syncHandle(row)">同步</zh-button>
          <zh-button plain size="small" color="#F56C6C" @click="deleteData(row)" style="color: red">删除</zh-button>
        </template>
      </zh-table>
    <el-drawer
      title="导入表"
      v-model="pageData.importVisible"
      destroy-on-close
      append-to-body
      size="70%"
    >
      <importForm
          ref="importFormRef"
          :disabled="pageData.showFlag"
          @canner="importcanner"
          @refreshTable="refreshTable"
          :id="pageData.selectRow.id"
      ></importForm>
      <template #footer>
				<span style="margin:0 10px">
					<zh-button @click="pageData.importVisible = false" type="info">关闭</zh-button>
					<zh-button
              type="primary"
              :loading="pageData.saveLoading"
              :disabled="pageData.saveLoading"
              v-model:btnLoding="pageData.saveLoading"
              @click="saveImport"
          >
						保存
					</zh-button>
				</span>
      </template>
    </el-drawer>
    <editForm ref="editRef"></editForm>
    <preview ref="previewRef"></preview>
    <generator ref="generatorRef" ></generator>
  </div>
</template>
<script lang="ts" setup name="generator">
import { ElMessageBox, ElMessage } from 'element-plus';
import importForm from './component/importForm.vue';
import editForm from './component/editForm.vue';
import preview from './component/preview.vue';
import generator from './component/generator.vue';
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
});

// ------------------定义子组件--------------------------
const queryTable = ref();
const queryFormRef = ref();
const importFormRef = ref();
const editRef=ref();
const previewRef=ref();
const generatorRef = ref();
const statusMap = {
  0: {
    status: 'error',
    text: '无效',
  },
  1: {
    status: 'processing',
    text: '有效',
  },
};

// ------------------变量区------------------------------
const pageData = reactive({
  advanced: false,
  toolbarConfig: {
    custom: true,
  },
  tableColumns: [
    {
      type: 'seq',
      width: 60,
      fixed: 'left',
      align: 'center',
      title: '序号',
    },
    {
      field: 'tableName',
      title: '表名',
      visible: true,
      align: 'center',
      minWidth: 150,
      sortable: true,
    },
    {
      field: 'tableComment',
      title: '说明',
      visible: true,
      align: 'center',
      minWidth: 200,
    },
    {
      field: 'className',
      title: '类名',
      visible: true,
      align: 'center',
      minWidth: 150,
    },
     
    {
      field: 'createTypeName',
      title: '生成类型',
      visible: true,
      align: 'center',
      minWidth: 150,
    },
    {
      field: 'operate',
      title: '操作',
      fixed: 'right',
      align: 'center',
      minWidth: 300,
      slots: {
        // 使用插槽模板渲染
        default: 'operate',
      },
    },
  ],
  queryParams: {} as any,
  dictData: {} as any,
  title: '',
  importVisible: false,
  showFlag: false,
  selectRow: {} as any,
  saveLoading: false,
});
// ------------------方法区------------------------------
const init = () => {
  refreshTable();
};

// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = '';
  $api.common.getBatchDict.post(params).then((res) => {
    pageData.dictData = res.result;
  });
};

//使用子组件方法，关闭弹窗
const importcanner = (e: any) => {
  pageData.importVisible = e;
};

const showAdd = () => {
  pageData.title = '导入表';
  pageData.importVisible = true;
};

//保存
const saveImport = () => {
  importFormRef.value.save();
};
const editHandle = (id?: number) => {
	editRef.value.init(id)
}
const previewHandle = (tableId?: number) => {
	previewRef.value.init(tableId)
}
const generatorHandle = (id?: number) => {
	generatorRef.value.init(id)
}//刷新
const refreshTable = () => {
  queryTable.value.refresh(pageData.queryParams);
};
//删除
const deleteData = (row) => {
  ElMessageBox.confirm('是否确认删除？').then(() => {
    let params = {} as any;
    params.id = row.id;
    $api.common.send.post('genTable0006', params).then(() => {
      ElMessage.success('删除成功');
      refreshTable();
    });
  });
};
const syncHandle = (row: any) => {
	ElMessageBox.confirm(`确定同步数据表${row.tableName}吗?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
      let params={id:row.id}
      $api.common.send.post('genTable0009', params).then(() => {
        ElMessage.success('同步成功')
        refreshTable();
      });
		})
		.catch(() => {})
}
</script>
<style lang="scss" scoped></style>
